<?php
// 验证菜单mig_src字段更新结果的脚本

// 数据库连接配置
$host = '127.0.0.1';
$dbname = 'oa5';
$username = 'root';
$password = '123456';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // 查询更新结果
    $sql = "SELECT COUNT(*) as total, 
                   COUNT(CASE WHEN mig_src != '' THEN 1 END) as updated,
                   COUNT(CASE WHEN mig_src = '' THEN 1 END) as not_updated
            FROM oa_admin_rule 
            WHERE src != '' AND menu = 1 AND status = 1";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    echo "菜单项更新结果统计:\n";
    echo str_repeat("-", 40) . "\n";
    echo "总菜单项数: " . $result['total'] . "\n";
    echo "已更新项数: " . $result['updated'] . "\n";
    echo "未更新项数: " . $result['not_updated'] . "\n";
    
    // 显示前10个更新的菜单项作为示例
    echo "\n前10个已更新的菜单项示例:\n";
    echo str_repeat("-", 40) . "\n";
    $sql2 = "SELECT id, title, src, mig_src 
             FROM oa_admin_rule 
             WHERE src != '' AND menu = 1 AND status = 1 AND mig_src != '' 
             ORDER BY id 
             LIMIT 10";
    
    $stmt2 = $pdo->prepare($sql2);
    $stmt2->execute();
    $items = $stmt2->fetchAll(PDO::FETCH_ASSOC);
    
    printf("%-5s %-15s %-25s %-30s\n", "ID", "标题", "原路径", "新路径");
    echo str_repeat("-", 85) . "\n";
    foreach ($items as $item) {
        printf("%-5s %-15s %-25s %-30s\n", 
               $item['id'], 
               mb_substr($item['title'], 0, 10), 
               mb_substr($item['src'], 0, 25), 
               mb_substr($item['mig_src'], 0, 30));
    }
    
    // 显示未更新的菜单项（如果有）
    $sql3 = "SELECT id, title, src 
             FROM oa_admin_rule 
             WHERE src != '' AND menu = 1 AND status = 1 AND mig_src = ''";
    
    $stmt3 = $pdo->prepare($sql3);
    $stmt3->execute();
    $unupdatedItems = $stmt3->fetchAll(PDO::FETCH_ASSOC);
    
    if (count($unupdatedItems) > 0) {
        echo "\n未更新的菜单项:\n";
        echo str_repeat("-", 60) . "\n";
        printf("%-5s %-15s %-30s\n", "ID", "标题", "原路径");
        echo str_repeat("-", 60) . "\n";
        foreach ($unupdatedItems as $item) {
            printf("%-5s %-15s %-30s\n", 
                   $item['id'], 
                   mb_substr($item['title'], 0, 10), 
                   $item['src']);
        }
    }
    
} catch(PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

echo "\n验证完成。\n";